<?php 
include("clases/Class_Session.php");
$Sess_add = new sesion();
include("clases/conex.php");
$link = conect();

if($_GET['log']){
	$Sess_add->limpiar();
	?><script>document.location.href = "login.php";</script><?php
}
if($_POST){
 if($_POST['login'] AND $_POST['clave']){
		
		$sql = "SELECT * FROM public.usuarios u
				LEFT JOIN public.datos_profesionales dp ON u.cod_usu = dp.cod_usu WHERE dp.log_usu = '$_POST[login]'";
				
				//print $sql; exit();
		$result = pg_query($link,$sql);	
		$row = pg_fetch_array($result);
		
		$sql2 = "SELECT cod_peri FROM periodos_escolares WHERE est_peri IS TRUE";
		$result2 = pg_query($link,$sql2);
		$row2 = pg_fetch_array($result2);
				
		if($row['cod_cat']==1)
		{
			if($row['pas_usu'] == md5($_POST['clave']))
			{
				
					
					$Sess_add->__set('userlog',true);
					$Sess_add->__set('userid',$row['cod_usu']);
					$Sess_add->__set('admin',true);
					$Sess_add->__set('usernombre',$row['log_usu']);
					$Sess_add->__set('periodo',$row2['cod_peri']);
					?><script>document.location.href = "main.php";</script><?php
			}
			else
			{
				?><script>document.location.href = "login.php?error=501";</script><?php
			}
		
		}
		else
		{
		
			if($row['pas_usu'] == md5($_POST['clave']))
			{
				$Sess_add->__set('userlog',true);
				$Sess_add->__set('userid',$row['cod_usu']);
				$Sess_add->__set('secret',true);
				$Sess_add->__set('usernombre',$row['log_usu']);
				$Sess_add->__set('periodo',$row2['cod_peri'])
				?><script>document.location.href = "main.php";</script><?php
			}
			else
			{
				?><script>document.location.href = "login.php?error=501";</script><?php
			}
		
		}
	}
}else
{
	?><script>document.location.href = "login.php?error=501";</script><?php
}

?>